//
//  ManagementTableView.m
//  YZZX
//
//  Created by Easaa on 15/5/7.
//  Copyright (c) 2015年 Easaa. All rights reserved.
//

#import "ManagementTableView.h"

#import "ManagementTableViewCell.h"

#import "ManagementDataModel.h"

#import "ReadTheMessageVIew.h"

@implementation ManagementTableView
{
    HXHttpRequest   *_httpRequest;
    NSMutableArray  *_DataManage;


}

- (id)initWithActionFantionFram:(CGRect)frame tyMessagebox:(NSString *)Messagebox
{
    self = [super initWithFrame:frame];
    if(self)
    {
        _DataManage = [[NSMutableArray alloc]init];
        self.Messagebox = Messagebox;
        
       //测试数据
       // self.Messagebox = @"Outbox";
        
        
        self.dataSource =self;
        self.delegate =self;
        UIView *v0 = [[UIView alloc] initWithFrame:CGRectZero];
        [self setTableFooterView:v0];
        
        __weak typeof(self) weakSelf = self;
        // 添加传统的下拉刷新,设置回调（一旦进入刷新状态就会调用这个refreshingBlock）
        [self addLegendHeaderWithRefreshingBlock:^{
            
            [weakSelf creatTableview];
        }];

        
        [self creatTableview];
    }
    return self;
}
-(void)creatTableview
{

    
    //列表请求
    NSDictionary* dicParams = [NSDictionary dictionaryWithObjectsAndKeys:
                               [XSZ_UICommon useridChangUaerdfLoginBoolString],                                @"user_id",
                               @"1",                                @"site_id",
                               [NSNumber numberWithInteger:1],      @"page_no",
                               [NSNumber numberWithInteger:10],     @"page_size",
                                self.Messagebox,                    @"messagebox",
                               nil];
    
   
    
    if (_httpRequest == nil)
    {
        _httpRequest = [[HXHttpRequest alloc] init];
        _httpRequest.delegate = self;
        
        
        
    }
    [_httpRequest requestForParams:dicParams andMethod:YZ_TOPICMESSAGES_TOPICLIST andTag:1023];//@"system.time"];//


}






#pragma mark - HXHttpRequestDelegate  代理

- (void)requestFinished:(NSString *)responseString andStatusCode:(int)statusCode andTag:(NSInteger)tag
{
    
    
    // 停止刷新状态
    [self.legendHeader endRefreshing];
    
    NSDictionary *dic = [[NSDictionary alloc]init];
    dic = [JsonUtil jsonToDic:responseString];
    
    if ([dic[@"data"] isEqual:[NSNull null]])
    {
        return;
    }
    
    NSString * strs = dic[@"data"];
    
    NSDictionary *dics =[JsonUtil jsonToDic:strs];
    
    NSArray *arryy =(NSArray *)dics;
    
    NSLog(@"-----------> %@",arryy);
    
    
    
    [_DataManage removeAllObjects];
    
    for (NSDictionary *dicuu in arryy)
    {
        ManagementDataModel  *modeContact =[[ManagementDataModel alloc]initWithDataManagement:dicuu];
        [_DataManage addObject:modeContact];
    }
    
    
    [self reloadData];
    
    
}


#pragma mark  HXHttpRequest - 请求代理，，错误代理
- (void)requestFailed:(NSError *)error andTag:(NSInteger)tag
{
    
    
    
}




#pragma arguments - mark  UITableViewDelegate
// 返回每组行数
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    
    
    return _DataManage.count;
}


//设置每行高度（每行高度可以不一样）
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    
    
    return 80;
}


-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    static  NSString  *CellIdentiferId = @"ManagementID";
    ManagementTableViewCell  *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentiferId];
    
    if (cell == nil) {
        NSArray *nibs = [[NSBundle mainBundle]loadNibNamed:@"ManagementTableViewCell" owner:nil options:nil];
        cell = [nibs lastObject];
        // cell.backgroundColor = [UIColor clearColor];
        
        
    }
    
    if(_DataManage.count<1)
    {
        return cell;
    }
    ManagementDataModel *cellmodel = [_DataManage objectAtIndex:indexPath.row];
    
    
    [cell.ManagementTitle setText:cellmodel.ReceiverName];
    
    
    
    //时间转换
    NSDateFormatter *dateformatter = [[NSDateFormatter alloc] init];
    [dateformatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
    NSDate *date = [dateformatter dateFromString:cellmodel.LastReplyTime];
    [dateformatter setDateFormat:@"yyyy-MM-dd"];
    NSString *strtime = [dateformatter stringFromDate:date];

    
    [cell.ManagementTime setText:strtime];//cellmodel.LastReplyTime];
    [cell.ManafementMassge setText:cellmodel.Title];
    
    
    return cell;
}


//响应点击事件

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    if(indexPath.row<_DataManage.count)
    {
        ManagementDataModel   *modeldata = [_DataManage objectAtIndex:indexPath.row];
        ReadTheMessageVIew  *readmassage =[[ReadTheMessageVIew alloc]init];
        readmassage.id_massge = modeldata.id_massge;
        [self.DelegatMessage.navigationController pushViewController:readmassage animated:YES];
    }
    
}



@end
